package com.bq.gametrade.view;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.R;

/**
 * 自定义按钮
 * Created by feel-wu on 2015/3/19.
 */
public class MyRadioButton {
    //按钮根视图
    public LinearLayout item_root;
    //按钮名称
    private TextView rg_item_tv;
    //按钮按下状态
    private ImageView rg_item_img_press;
    //按钮正常状态
    private ImageView rg_item_img_normal;
    //按钮编号
    public int radio_group_item_id;

    /**
     * @param nomalId 按钮正常状态图片ID
     * @param pressId 按钮按下状态图片ID
     * @param name    按钮名称
     * @param c       上下文
     */
    public MyRadioButton(int nomalId, int pressId, String name, int radio_group_item_id, Context c) {
        this.radio_group_item_id = radio_group_item_id;
        LayoutInflater inflater = LayoutInflater.from(c);
        View radio_group_item = inflater.inflate(R.layout.radio_group_item, null);
        item_root = (LinearLayout) radio_group_item.findViewById(R.id.item_root);
        LinearLayout.LayoutParams itemLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        itemLayoutParams.weight = 1;
        radio_group_item.setLayoutParams(itemLayoutParams);
        rg_item_img_normal = (ImageView) radio_group_item.findViewById(R.id.rg_item_img_normal);
        rg_item_img_press = (ImageView) radio_group_item.findViewById(R.id.rg_item_img_press);
        rg_item_tv = (TextView) radio_group_item.findViewById(R.id.rg_item_tv);
        rg_item_img_normal.setImageResource(nomalId);
        rg_item_img_press.setImageResource(pressId);
        rg_item_tv.setText(name);
        setChecked(false);
    }

    /**
     * 设置按钮点击样式
     *
     * @param isChecked 是否点击
     */
    void setChecked(boolean isChecked) {
        if (isChecked) {
            setButtonNormalAlpha(0f);
        } else {
            setButtonNormalAlpha(1f);
        }
    }

    void setButtonNormalAlpha(float normalValue) {
        rg_item_img_normal.setAlpha(normalValue);
        rg_item_img_press.setAlpha(1f - normalValue);
        if(normalValue<1f - normalValue){
            rg_item_tv.setTextColor(Color.parseColor("#1194e4"));
        }else {
            rg_item_tv.setTextColor(Color.parseColor("#777777"));
        }
    }

}
